package it.tcm.TeacherClassMate.facade;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import it.tcm.TeacherClassMate.beans.OrderRowBean;
import it.tcm.TeacherClassMate.dao.OrderRowDao;
import it.tcm.TeacherClassMate.persistent.HibernateUtil;


public class OrderRowManageBase {

	 	private Logger logger = Logger.getLogger(this.getClass().getName());   
	    private OrderRowDao eDao = new OrderRowDao();
	  
	    public OrderRowBean getOrderRowById(Integer id) {
		    Session session = null;
		    OrderRowBean o = null;
			try {
				session = HibernateUtil.createNewSessionAndTransaction();
				eDao.setSession(session);
				o = eDao.findById(id, false);
			    HibernateUtil.commitTransactionAndCloseSession(session);
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Error in reading orderRow "  + id.toString(), e);
			} finally {
			}
			return o;
	    }
	    
	   
	    
	    public Integer save(OrderRowBean o)
	    {
	  	  Session session = null;
	  	  try 
	  	  {
	  		session = HibernateUtil.createNewSessionAndTransaction();
	  		eDao.setSession(session);
	  		eDao.attachDirty(o);
	  		HibernateUtil.commitTransactionAndCloseSession(session);
	  	  } 
	  	  catch (Exception e) 
	  	  {
	  		  logger.error("Errore in save orderrow ", e);
	  		  HibernateUtil.rollbackTransaction(session);
	  	  } 	   	  
	  	  return o.getId();
	    }
	    
	    
	    public boolean delete(OrderRowBean o) {  
	  	  Session session = null;
	  	  try 
	  	  {
	  		session = HibernateUtil.createNewSessionAndTransaction();
	  		eDao.setSession(session);
	  		eDao.delete(o);
	  		HibernateUtil.commitTransactionAndCloseSession(session);
	  	  } 
	  	  catch (Exception e) 
	  	  {
	  		  logger.error("Errore in delete orderRow ", e);
	  		  HibernateUtil.rollbackTransaction(session);
	  	  } 	   	  
	  	  return true;
	    }
	    
	    public List<OrderRowBean> getAll() {
		    Session session = null;
		    List<OrderRowBean> eList = null;
			try {
				session = HibernateUtil.createNewSessionAndTransaction();
				eDao.setSession(session);
				eList = eDao.findAll();
			    HibernateUtil.commitTransactionAndCloseSession(session);
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    logger.error("Errore in reading all orderRow ", e);
			} finally {
			}
			return eList;
	    }
	   
	    
	  
}
